<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="warning" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input clearable size="medium" v-model="params.kwd" placeholder="请输入手机号或用户名" class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data" border>
                        <el-table-column align="center" type="selection" width="30"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column align="center" prop="thumb" label="封面" width="80">
                            <template slot-scope="scope">
                                <img :src="scope.row.avatar" style="height: 60px;width: 60px;border-radius: 50%"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="username" label="手机号" width="120"></el-table-column>
                        <el-table-column prop="name" label="用户名" width="120"></el-table-column>
                        <el-table-column align="center" prop="integral" label="积分" width="60"></el-table-column>
                        <el-table-column prop="company" label="公司" width="120" :show-overflow-tooltip="true"></el-table-column>
                        <el-table-column prop="email" label="邮箱" width="150" :show-overflow-tooltip="true"></el-table-column>
                        <el-table-column prop="login_ip" label="登录IP" width="100"></el-table-column>
                        <el-table-column prop="login_at" label="最近登陆" :show-overflow-tooltip="true" width="160"></el-table-column>
                        <el-table-column prop="register_ip" label="注册IP" width="100"></el-table-column>
                        <el-table-column prop="created_at" label="注册日期" width="160"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button @click="destory(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" :rules="rules" ref="form" label-width="120px">
                <el-form-item required label="头像："  :rules="[{ required: true, message: '请上传用户头像', trigger: 'change'}]" prop="avatar">
                    <el-upload
                            class="index-avatar-uploader"
                            :action="upload"
                            :show-file-list="false"
                            name="image"
                            :on-success="uploadSuccess"
                            :before-upload="uploadBefore">
                        <img v-if="form.avatar" :src="form.avatar"
                             class="index-avatar" style="width: 120px;height: 120px;border-radius: 50%">
                        <i v-else class="el-icon-plus index-avatar-uploader-icon" style="width: 120px;height: 120px;line-height: 120px;border-radius: 50%"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="手机号：" prop="username">
                    <el-input  v-model="form.username" readonly placeholder="请填写用户手机号"></el-input>
                </el-form-item>
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="form.name" placeholder="请填写用户名称"></el-input>
                </el-form-item>
                <el-form-item label="公司：" prop="company">
                    <el-input v-model="form.company" placeholder="请填写公司名称"></el-input>
                </el-form-item>
                <el-form-item label="邮箱：" prop="email">
                    <el-input v-model="form.email" placeholder="请填写邮箱"></el-input>
                </el-form-item>
                <el-form-item label="个人简介：" prop="introduce">
                    <el-input type="textarea" rows="5" v-model="form.introduce" placeholder="请填写个人简介"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update('form')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {UserList,UserUpdate,UserDestroy} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page:1,kwd:''},
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '650px'
                },
                multipleSelection:[],//多选项
                form:null,
            }
        },
        activated(){
            this.list();
        },
        computed: {
            ...mapGetters(['active','loading','upload']),
        },
        methods: {
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    name: info.name,
                    username: info.username,
                    avatar: info.avatar,
                    company: info.company,
                    introduce: info.introduce,
                    email: info.email,
                    position: info.position,
                };
                this.dialog.title = '编辑用户';
                this.dialog.show = true;
            },
            //上传封面
            uploadSuccess(res, file) {
                this.form.avatar = res.data.path;
            },
            uploadBefore(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传封面图片只能是 JPG | PNG | GIF格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传封面图片大小不能超过 2M!');
                }
                return isJPG && isLt2M;
            },
            //筛选
            search(){
                this.params.page = 1;
                this.list();
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                UserList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        UserUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            // 多选删除
            multiDelete(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要删除的项');return false;
                }
                this.destroy(this.multipleSelection);
            },
            handleSelectionChange(val) {
                let select = [];
                if(val.length > 0){
                    val.filter((item)=>{
                        if(select.indexOf(item.id) === -1){
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                }else{
                    this.multipleSelection = [];
                }
            },
            //删除
            destroy(id){
                this.$confirm('确定要删除选中的用户信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    UserDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
        }
    }
</script>

<style scoped>

</style>
